package edu.cicese.alz;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 22/11/11
 * Time: 02:26 PM
 */

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class TimeUtil {
	public static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
	public static SimpleDateFormat sdfFolder = new SimpleDateFormat("yyyy/MM/dd");

    public static String getSQLDate(long timestamp) {
//		DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS");
		DateFormat sqlDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

	    Calendar calendar = Calendar.getInstance();
	    calendar.setTimeInMillis(timestamp);

	    return "'" + sqlDateFormat.format(calendar.getTime()) + "'";
    }

	public static long getDateInMillis(String date) {
		Calendar cal = Calendar.getInstance();
		try {
			cal.setTime(sdf.parse(date));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return cal.getTimeInMillis();
	}

	public static Date getDateFromMs(long millisecond) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(millisecond);
		return cal.getTime();
	}

	public static String getStringDateFromMs(long millisecond, SimpleDateFormat sdf) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(millisecond);
		return sdf.format(cal.getTime());
	}

	public static int getDayDifference(long ts1, long ts2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTimeInMillis(ts1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTimeInMillis(ts2);

		Calendar calBegin = new GregorianCalendar();
		Calendar calEnd = new GregorianCalendar();

		calBegin.set(cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH), cal1.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		calEnd.set(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH), cal2.get(Calendar.DAY_OF_MONTH), 24, 0, 0);

		return (int)((calEnd.getTimeInMillis() - calBegin.getTimeInMillis()) / 86400000);
	}

	public static Calendar getFirstDay(long d) {
		Calendar cal = new GregorianCalendar();
		cal.setTimeInMillis(d);

		Calendar calSimplified = Calendar.getInstance();
		calSimplified.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);

		return calSimplified;
	}

	/*public static Object getDay(long d) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(d);

		Object day = new Day();


		Calendar calSimplified = new GregorianCalendar();
		calSimplified.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
	}*/

	public static void main(String[] args) {
		String date = "24/11/2011 04:00:01";
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		try {
			cal.setTime(sdf.parse(date));
			System.out.println(cal.getTimeInMillis());
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}